package com.vallis.web.controller.system;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vallis.common.core.domain.entity.SysUser;
import com.vallis.lottery.domain.SnNumber;
import com.vallis.lottery.service.ISnNumberService;
import com.vallis.system.service.ISysUserService;
import com.vallis.web.service.SysRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.vallis.common.core.controller.BaseController;
import com.vallis.common.core.domain.AjaxResult;
import com.vallis.common.core.domain.model.RegisterBody;
import com.vallis.common.utils.StringUtils;
import com.vallis.system.service.ISysConfigService;

import java.util.Date;

/**
 * 注册验证
 * 
 * @author vallis
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISnNumberService snNumberService;

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
    System.out.println(user);
        System.out.println(user.getAskCode());
        System.out.println("用户注册");

        String msg ="";
        SnNumber one = snNumberService.getOne(new QueryWrapper<SnNumber>().eq("sn_number",user.getAskCode()));
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        if (null!=one){
            if (one.getOffTime().after(new Date())){

                if(one.getIsUse()==0){
                    registerService.register(user);

                    SysUser sysUser = userService.selectUserByUserName(user.getUsername());


                    one.setUseBy(sysUser.getUserId());
                    one.setIsUse(1);
                    snNumberService.updateById(one);
                    return success("恭喜你，已经成功注册");
                }else {
                    return error("抱歉，邀请码已被使用");

                }


            }else {
                return error("抱歉，邀请码已过期");

            }

        }else {
            return error("抱歉，邀请码不正确");
        }






    }
}
